package basic;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2021/8/13 10:08
 */
public class Title4 {
    public static void main(String[] args) {
        int[] a={1,2};
        int[] b={3,4};
        System.out.println(findMedianSortedArrays(a,b));
    }

    private static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int m =nums1.length;
        int n = nums2.length;
        int left=0, right=0;
        int len = m+n;
        int aStart=0, bStart=0;
        for (int i = 0; i <= len/2; i++) {
            left=right;
            //数组A aStart不能越界，且保证bStart不能越界
            if(aStart<m && (bStart>=n || nums1[aStart]<=nums2[bStart])){
                right = nums1[aStart++];
            }else{
                right = nums2[bStart++];
            }
        }
        if(len%2==0){
            return (left + right) / 2.0;
        }else{
            return right;
        }
    }
}
